jetcrab\vm\types/
indices.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
5pub struct ConstantIndex(usize);
6
7impl ConstantIndex {
8    pub fn new(index: usize) -> Self {
9        Self(index)
10    }
11
12    pub fn as_usize(&self) -> usize {
13        self.0
14    }
15}
16
17impl From<usize> for ConstantIndex {
18    fn from(index: usize) -> Self {
19        Self(index)
20    }
21}
22
23impl From<ConstantIndex> for usize {
24    fn from(idx: ConstantIndex) -> Self {
25        idx.0
26    }
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
30pub struct GlobalIndex(usize);
31
32impl GlobalIndex {
33    pub fn new(index: usize) -> Self {
34        Self(index)
35    }
36
37    pub fn as_usize(&self) -> usize {
38        self.0
39    }
40}
41
42impl From<usize> for GlobalIndex {
43    fn from(index: usize) -> Self {
44        Self(index)
45    }
46}
47
48impl From<GlobalIndex> for usize {
49    fn from(idx: GlobalIndex) -> Self {
50        idx.0
51    }
52}
53
54#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
55pub struct LocalIndex(usize);
56
57impl LocalIndex {
58    pub fn new(index: usize) -> Self {
59        Self(index)
60    }
61
62    pub fn as_usize(&self) -> usize {
63        self.0
64    }
65}
66
67impl From<usize> for LocalIndex {
68    fn from(index: usize) -> Self {
69        Self(index)
70    }
71}
72
73impl From<LocalIndex> for usize {
74    fn from(idx: LocalIndex) -> Self {
75        idx.0
76    }
77}
78
79#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
80pub struct ArgIndex(usize);
81
82impl ArgIndex {
83    pub fn new(index: usize) -> Self {
84        Self(index)
85    }
86
87    pub fn as_usize(&self) -> usize {
88        self.0
89    }
90}
91
92impl From<usize> for ArgIndex {
93    fn from(index: usize) -> Self {
94        Self(index)
95    }
96}
97
98impl From<ArgIndex> for usize {
99    fn from(idx: ArgIndex) -> Self {
100        idx.0
101    }
102}
103
104#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
105pub struct FunctionIndex(usize);
106
107impl FunctionIndex {
108    pub fn new(index: usize) -> Self {
109        Self(index)
110    }
111
112    pub fn as_usize(&self) -> usize {
113        self.0
114    }
115}
116
117impl From<usize> for FunctionIndex {
118    fn from(index: usize) -> Self {
119        Self(index)
120    }
121}
122
123impl From<FunctionIndex> for usize {
124    fn from(idx: FunctionIndex) -> Self {
125        idx.0
126    }
127}
128
129impl fmt::Display for FunctionIndex {
130    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
131        write!(f, "Function({})", self.0)
132    }
133}
134
135#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
136pub struct ArraySize(usize);
137
138impl ArraySize {
139    pub fn new(size: usize) -> Self {
140        Self(size)
141    }
142
143    pub fn as_usize(&self) -> usize {
144        self.0
145    }
146}
147
148impl From<usize> for ArraySize {
149    fn from(size: usize) -> Self {
150        Self(size)
151    }
152}
153
154impl From<ArraySize> for usize {
155    fn from(size: ArraySize) -> Self {
156        size.0
157    }
158}
159
160#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
161pub struct StackIndex(usize);
162
163impl StackIndex {
164    pub fn new(index: usize) -> Self {
165        Self(index)
166    }
167
168    pub fn as_usize(&self) -> usize {
169        self.0
170    }
171
172    pub fn increment(&mut self) {
173        self.0 += 1;
174    }
175
176    pub fn decrement(&mut self) {
177        if self.0 > 0 {
178            self.0 -= 1;
179        }
180    }
181}
182
183impl From<usize> for StackIndex {
184    fn from(index: usize) -> Self {
185        Self(index)
186    }
187}
188
189impl From<StackIndex> for usize {
190    fn from(idx: StackIndex) -> Self {
191        idx.0
192    }
193}
194
195#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
196pub struct FramePointer(usize);
197
198impl FramePointer {
199    pub fn new(pointer: usize) -> Self {
200        Self(pointer)
201    }
202
203    pub fn as_usize(&self) -> usize {
204        self.0
205    }
206}
207
208impl From<usize> for FramePointer {
209    fn from(pointer: usize) -> Self {
210        Self(pointer)
211    }
212}
213
214impl From<FramePointer> for usize {
215    fn from(ptr: FramePointer) -> Self {
216        ptr.0
217    }
218}
219
220#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
221pub struct ObjectId(usize);
222
223impl ObjectId {
224    pub fn new(id: usize) -> Self {
225        Self(id)
226    }
227
228    pub fn as_usize(&self) -> usize {
229        self.0
230    }
231
232    pub fn is_valid(&self) -> bool {
233        self.0 != usize::MAX
234    }
235}
236
237impl From<usize> for ObjectId {
238    fn from(id: usize) -> Self {
239        Self(id)
240    }
241}
242
243impl From<ObjectId> for usize {
244    fn from(id: ObjectId) -> Self {
245        id.0
246    }
247}
248
249impl fmt::Display for ObjectId {
250    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
251        write!(f, "Object({})", self.0)
252    }
253}
254
255#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
256pub struct HeapId(usize);
257
258impl HeapId {
259    pub fn new(id: usize) -> Self {
260        Self(id)
261    }
262
263    pub fn as_usize(&self) -> usize {
264        self.0
265    }
266
267    pub fn is_valid(&self) -> bool {
268        self.0 != usize::MAX
269    }
270
271    pub fn increment(&mut self) {
272        self.0 += 1;
273    }
274
275    pub fn next(&self) -> Self {
276        Self(self.0 + 1)
277    }
278}
279
280impl From<usize> for HeapId {
281    fn from(id: usize) -> Self {
282        Self(id)
283    }
284}
285
286impl From<HeapId> for usize {
287    fn from(id: HeapId) -> Self {
288        id.0
289    }
290}
291
292impl fmt::Display for HeapId {
293    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
294        write!(f, "Heap({})", self.0)
295    }
296}